package com.yeeverse.sdk.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.yeeverse.sdk.exception.SdkException;
import com.yeeverse.sdk.vo.api.BaseRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Map;

/**
 * @Author: XiongGaoXiang
 * @Date: 2022/7/4
 */
public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final ObjectWriter writer;
    private static final ObjectWriter prettyWriter;

    static {
        objectMapper.configure(Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        writer = objectMapper.writer();
        prettyWriter = objectMapper.writerWithDefaultPrettyPrinter();
    }

    public JsonUtils() {
    }

    public static String toJsonPrettyString(Object value) throws JsonProcessingException {
        return prettyWriter.writeValueAsString(value);
    }

    public static String toJsonString(Object value) {
        try {
            return writer.writeValueAsString(value);
        } catch (Exception var2) {
            throw new IllegalStateException(var2);
        }
    }

    public static <T> T fromJsonString(String json, Class<T> clazz) {
        if (json == null) {
            return null;
        } else {
            try {
                return objectMapper.readValue(json, clazz);
            } catch (Exception var3) {
                throw new RuntimeException("Unable to parse Json String.", var3);
            }
        }
    }

    public static <T> T fromJsonString(String json, TypeReference<T> typeReference) {
        if (json == null) {
            return null;
        } else {
            try {
                return objectMapper.readValue(json, typeReference);
            } catch (Exception var3) {
                throw new SdkException("Unable to parse Json String.", var3);
            }
        }
    }

    public static JsonNode jsonNodeOf(String json) {
        return (JsonNode) fromJsonString(json, JsonNode.class);
    }

    public static JsonGenerator jsonGeneratorOf(Writer writer) throws IOException {
        return (new JsonFactory()).createGenerator(writer);
    }

    public static <T> T loadFrom(File file, Class<T> clazz) throws IOException {
        try {
            return objectMapper.readValue(file, clazz);
        } catch (IOException var3) {
            throw var3;
        } catch (Exception var4) {
            throw new IllegalStateException(var4);
        }
    }

    public static void load(InputStream input, Object obj) throws IOException, JsonProcessingException {
        objectMapper.readerForUpdating(obj).readValue(input);
    }

    public static <T> T loadFrom(InputStream input, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(input, clazz);
    }

    /**
     * 实体转化为Map
     *
     * @param request
     * @return
     */
    public static Map<String, Object> parseBean2Map(BaseRequest request) {
        return JsonUtils.fromJsonString(JsonUtils.toJsonString(request), new TypeReference<Map<String, Object>>() {
        });
    }
}
